﻿using System;
using System.Reflection;
using System.Windows.Forms;
using ResxCrunch.Properties;
using ResxCrunch.Business.Managers;
using ResxCrunch.Utils;

namespace ResxCrunch
{
    /// <summary>
    /// About form class
    /// </summary>
    public partial class FrmAbout : Form
    {
        #region Properties
        /// <summary>
        /// Gets or sets the registration information.
        /// </summary>
        /// <value>The register info.</value>
        public RegistrationData RegisterInfo { get; set; }
        #endregion Properties

        /// <summary>
        /// Initializes a new instance of the <see cref="FrmAbout"/> class.
        /// </summary>
        public FrmAbout()
        {
            this.InitializeComponent();
            this.Text = String.Format("About {0} {0}", this.AssemblyTitle);
            this.labelProductName.Text = this.AssemblyProduct;
            this.labelVersion.Text = String.Format("Version {0}", this.AssemblyVersion);
            this.labelCopyright.Text = this.AssemblyCopyright;
            this.labelCompanyName.Text = this.AssemblyCompany;
            this.textBoxDescription.Text = this.AssemblyDescription;
        }

        #region Assembly Attribute Accessors

        /// <summary>
        /// Gets the assembly title.
        /// </summary>
        /// <value>The assembly title.</value>
        public string AssemblyTitle
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);

                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (titleAttribute.Title != string.Empty)
                    {
                        return titleAttribute.Title;
                    }
                }

                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        /// <summary>
        /// Gets the assembly version.
        /// </summary>
        /// <value>The assembly version.</value>
        public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        /// <summary>
        /// Gets the assembly description.
        /// </summary>
        /// <value>The assembly description.</value>
        public string AssemblyDescription
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);

                if (attributes.Length == 0)
                {
                    return string.Empty;
                }

                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        /// <summary>
        /// Gets the assembly product.
        /// </summary>
        /// <value>The assembly product.</value>
        public string AssemblyProduct
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);

                if (attributes.Length == 0)
                {
                    return string.Empty;
                }

                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        /// <summary>
        /// Gets the assembly copyright.
        /// </summary>
        /// <value>The assembly copyright.</value>
        public string AssemblyCopyright
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);

                if (attributes.Length == 0)
                {
                    return string.Empty;
                }

                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        /// <summary>
        /// Gets the assembly company.
        /// </summary>
        /// <value>The assembly company.</value>
        public string AssemblyCompany
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);

                if (attributes.Length == 0)
                {
                    return string.Empty;
                }

                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }
        #endregion

        /// <summary>
        /// Handles the Load event of the FrmAbout control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void FrmAbout_Load(object sender, EventArgs e)
        {
            this.LoadRegistrationInfo();

            this.Text = string.Format("About {0}", Settings.Default.AppName);
        }

        /// <summary>
        /// Loads the registration info from the license file.
        /// </summary>
        private void LoadRegistrationInfo()
        {
            this.RegisterInfo = SerialMngr.Instance.ExtractRegistrationDataFromFile(Settings.Default.DataFilePath, false);

            if (this.RegisterInfo != null && !this.RegisterInfo.Email.Equals(Utils.Constants.TrialEmailAddress, StringComparison.InvariantCultureIgnoreCase))
            {
                txtRegisteredUserEmail.Text = this.RegisterInfo.Email;
                txtRegisteredDate.Text = this.RegisterInfo.RegistrationDate.ToString();
            }
            else
            {
                lblRegisteredState.Text = "UNREGISTERED VERSION";
                txtRegisteredUserEmail.Visible = false;

                lblRegisteredOn.Visible = false;
                txtRegisteredDate.Visible = false;
            }
        }
    }
}
